//
//  UIBarButtonItem+Extension.swift
//  WeiBo18
//
//  Created by apple on 15/10/9.
//  Copyright © 2015年 apple. All rights reserved.
//

import Foundation
import UIKit

let SCREENW = UIScreen.mainScreen().bounds.size.width
let SCREENH = UIScreen.mainScreen().bounds.size.height


extension UIBarButtonItem {
    class func item(imageName: String = "", title: String = "", target: AnyObject?, action: Selector) -> UIBarButtonItem {
        //初始化一个button
        let button = UIButton()
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        //判断是否有title
        if title.characters.count>0 {
            //设置文字以及字体颜色,以及字体大小
            button.setTitle(title, forState: UIControlState.Normal)
            button.titleLabel?.font = UIFont.systemFontOfSize(14)
            //默认的颜色
            
            button.setTitleColor(UIColor(red: 80/255, green: 80/255, blue: 80/255, alpha: 1), forState: UIControlState.Normal)
            //高亮的颜色
            button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Highlighted)
        }
        
        if imageName.characters.count>0 {
        
            //设置button的不同状态的图片
            button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
            //高亮的图片
            button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        }
        //调整大小
        button.sizeToFit()
        
        return UIBarButtonItem(customView: button)
    }
    
//    class func item(imageName: String,title: String, target: AnyObject?, action: Selector) -> UIBarButtonItem {
//        //初始化一个button
//        let button = UIButton()
//        
//        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
//        //设置按钮的文字
//        button.setTitle(title, forState: UIControlState.Normal)
//        
//        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
//        
//        //设置button的不同状态的图片
//        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
//        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
//        //调整大小
//        button.sizeToFit()
//        
//        return UIBarButtonItem(customView: button)
//    }
}